package com.example.e_comm.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.mapper.PermissionMapper;
import com.example.e_comm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionServiceImpl
        extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {
    @Autowired
    private PermissionMapper mapper;

    @Override
    public List<Permission> getPermissionByPage(Page<Permission> page) {
        return mapper.selectPage(page, null).getRecords();
    }

    @Override
    public IPage<Permission> getPermissionByPage(Page<Permission> page, String keyword) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if(StringUtils.isNumber(keyword)){
            wrapper.like("perm_name",keyword)
                    .or().like("perm_key",keyword);
        }
        return mapper.selectPage(page,wrapper);
    }

    @Override
    public List<Permission> getPermissionByUserId(Long userId) {
        return mapper.getPermissionByUserId(userId);
    }

    @Override
    public List<Permission> getPermissionsByRoleId(Long roleId) {
        return mapper.getPermissionByRoleId(roleId);
    }

    @Override
    public Set<String> getPermissionByUserIdSet(Long userId){
        List<Permission> list = getPermissionByUserId(userId);
        return list.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }

    @Override
    public Set<String> getPermissionByRoleIdSet(Long roleId){
        List<Permission> list = getPermissionsByRoleId(roleId);
        return list.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }

    // 注意: 下面这些方法如果没有在 PermissionService 接口中声明，应该删除
    @Override
    public boolean saveOrUpdate(Permission entity) {
        return this.saveOrUpdate(entity);
    }

    @Override
    public Permission getOne(QueryWrapper<Permission> queryWrapper, boolean throwEx) {
        return this.getOne(queryWrapper, throwEx);
    }

    @Override
    public Optional<Permission> getOneOpt(QueryWrapper<Permission> queryWrapper, boolean throwEx) {
        try {
            Permission permission = this.getOne(queryWrapper, throwEx);
            return Optional.ofNullable(permission);
        } catch (Exception e) {
            if (throwEx) {
                throw e;
            }
            return Optional.empty();
        }
    }

    @Override
    public Map<String, Object> getMap(QueryWrapper<Permission> queryWrapper) {
        Permission permission = this.getOne(queryWrapper, false);
        if (permission != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", permission.getId());
            map.put("permName", permission.getPermName());
            map.put("permKey", permission.getPermKey());
            // 添加其他需要的字段
            return map;
        }
        return Collections.emptyMap();
    }

    @Override
    public <V> V getObj(QueryWrapper<Permission> queryWrapper, Function<? super Object, V> mapper) {
        Permission permission = this.getOne(queryWrapper, false);
        return mapper.apply(permission);
    }

    @Override
    public List<Permission> getPermissionByRoleId(Long roleId) {
        return null;
    }
}
